package org.evelsoft.ejb.mdbBeans;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.jms.JMSException;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.evelsoft.common.utils.Logger;
import org.evelsoft.common.dto.MessageJMS;

/**
 *
 * @author wojtek
 */
@MessageDriven(mappedName = "jms/evelTopic",
activationConfig = {
	/*        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
	@ActivationConfigProperty(propertyName = "connectionFactoryJndiName", propertyValue = "jms/ontoConnectionFactory"),*/
	@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
	@ActivationConfigProperty(propertyName = "destinationName", propertyValue = "jms/evelTopic")
})
public class TestMessageBean implements MessageListener {

	Logger logger = Logger.getInstance(TestMessageBean.class);
	private static int invoked = 0;
	private Integer beanInvoked = 0;

	//@Resource
	//MessageDrivenContext msgContext;
	public TestMessageBean() {
	}

	@Override
	public void onMessage(Message message) {
		logger.debug(Logger.START, "testOnMessage");

		ObjectMessage objectMessage = (ObjectMessage) message;
		try {
			MessageJMS msg = (MessageJMS) objectMessage.getObject();
			Long time = System.currentTimeMillis() - msg.getTime();
			logger.debug(Logger.START, "testOnMessage->" + time);
		} catch (JMSException ex) {
			logger.debug(ex);
		}
		logger.debug(Logger.STOP, "testOnMessage");
	}

	@PostConstruct
	public void inicjalize() {

		beanInvoked = 0;
		System.out.println(TestMessageBean.class + " inicjalized" + "-" + beanInvoked + "-" + invoked + "]");
	}
	/*
	@PostActivate
	public void activated(){
	System.out.println(TestMessageBean.class + " active" + "-" + beanInvoked + "-" + invoked + "]");
	}

	@PrePassivate
	public void passived(){
	System.out.println(TestMessageBean.class + " passive" + "-" + beanInvoked + "-" + invoked + "]");
	}
	 */

	@PreDestroy
	public void destroyed() {
		System.out.println(TestMessageBean.class + " destroyed" + "-" + beanInvoked + "-" + invoked + "]");
	}
}
